package com.shengyin.ec.util;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadController {
	private final int IMAGE_SIZE = 120;  
	private String UPLOADIR="upload";//上传文件路径

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public String handleFormUpload(HttpServletRequest request,
			@RequestParam("file") MultipartFile file) throws IOException {
		if(!file.isEmpty()){
			
			String fileName = file.getOriginalFilename(); 
			
			/** 
			                     
			* 获取文件后缀 
		                   
			*/
		                   
			String[] suffixs=fileName.split("\\."); 
		                    
			String suffix = "."+suffixs[suffixs.length-1];
			
			byte[] bytes = file.getBytes(); 
			String uploadPath = "upload/"+fileName; 
			File filePath = new File(request.getSession().getServletContext().getRealPath(uploadPath)); 
			if(!filePath.getParentFile().exists()){
				filePath.getParentFile().mkdirs();
			}
			FileCopyUtils.copy(bytes, filePath);
			String preview="preview";
			createPreviewImage(uploadPath,uploadPath);
		
		}
		return "redirect:index.jsp";
	}

	public static void createPreviewImage(String srcFile, String destFile) {   
        try {   
            File fi = new File(srcFile); // src   
            File fo = new File(destFile); // dest   
            BufferedImage bis = ImageIO.read(fi);   
            final int IMAGE_SIZE = 120;  
            int w = bis.getWidth();   
            int h = bis.getHeight();   
            double scale = (double) w / h;   
            int nw = IMAGE_SIZE; // final int IMAGE_SIZE = 120;   
            int nh = (nw * h) / w;   
            if (nh > IMAGE_SIZE) {   
                nh = IMAGE_SIZE;   
                nw = (nh * w) / h;   
            }   
            double sx = (double) nw / w;   
            double sy = (double) nh / h;   
  
            AffineTransform transform=new  AffineTransform();
            transform.setToScale(sx, sy);   
            AffineTransformOp ato = new AffineTransformOp(transform, null);   
            BufferedImage bid = new BufferedImage(nw, nh,   
                    BufferedImage.TYPE_3BYTE_BGR);   
            ato.filter(bis, bid);   
            ImageIO.write(bid, " jpeg ", fo);   
        } catch (Exception e) {   
            e.printStackTrace();   
            throw new RuntimeException(   
                    " Failed in create preview image. Error:  "  
                            + e.getMessage());   
        }   
    }  
}
